package com.xcd.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xcd.reggie.common.R;
import com.xcd.reggie.entity.Category;
import com.xcd.reggie.service.CategoryService;
import com.xcd.reggie.service.DishService;
import com.xcd.reggie.service.SetmealService;
import com.xcd.reggie.service.impl.CategoryServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
//    @Autowired
//    private DishService dishService;
//    @Autowired
//    private SetmealService setmealService;

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize){
        log.info("page：{} , pageSize: {}",page,pageSize);
        Page<Category> categoryPage = new Page<Category>(page,pageSize);
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.orderByAsc(Category::getSort);
        categoryService.page(categoryPage,lambdaQueryWrapper);
        return R.success(categoryPage);
    }

    @PostMapping
    public R<String> add(@RequestBody Category category, HttpServletRequest request){
        Long employeeId =(Long) request.getSession().getAttribute("employee");
        log.info("菜品分类信息： {}, 操作者：{}",category.toString(),employeeId);
//        category.setCreateUser(employeeId);
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
//        category.setUpdateUser(employeeId);
//        category.setIsDeleted(1);
        categoryService.save(category);
        return R.success("添加成功");
    }
    @PutMapping
    public R<String> update(@RequestBody Category category){
        log.info("更新的信息：{}",category);
        categoryService.updateById(category);
        return R.success("修改成功");
    }
    @DeleteMapping
    public  R<String> delete(Long ids){
        log.info("ids：{}",ids);
        categoryService.remove(ids);
        return R.success("删除成功");
    }

    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper();

        lambdaQueryWrapper.eq(category.getType() != null,Category::getType,category.getType());

        lambdaQueryWrapper.orderByAsc(Category::getSort).orderByAsc(Category::getUpdateTime);

        List<Category> list = categoryService.list(lambdaQueryWrapper);
        return  R.success(list);

    }

}
